package com.ljx.reflect.reflect02;

import com.sun.org.apache.xpath.internal.objects.XString;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/10/25 22:05
 * @Version 1.0
 */
public class Test {
	public static void main(String[] args) throws Exception {
		// 通过类名获取
		Class<?> aClass = Class.forName("com.ljx.reflect.reflect02.Student");
		Object o = aClass.newInstance();// 调用的是无参的构造函数

		Annotation[] annotations = o.getClass().getAnnotations();
		for (Annotation annotation : annotations) {
			System.out.println(annotation);
		}

		A annotation = aClass.getAnnotation(A.class);
		System.out.println(annotation);

		Field age = aClass.getDeclaredField("age");
		A annotation1 = age.getAnnotation(A.class);
		System.out.println(annotation1.value());
	}
}

// 自定义注解
@Retention(RetentionPolicy.RUNTIME)
@interface  A{
	int value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface B{
	String name();
}


@A(25)
@B(name="ljx")
class Student{

	private String name;
	@A(value = 26)
	protected int age;
}

